package com.zhengxyou.jetpackdemo.dataBinding;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

import androidx.databinding.BindingConversion;

public class BindingConverters {
//    @BindingConversion
//    public static ColorDrawable convertColorToDrawable(String color) {
//        return new ColorDrawable(Color.parseColor(color));
//    }

    @BindingConversion
    public static ColorDrawable convertColorToDrawable(int color) {
        return new ColorDrawable(color);
    }

    //BindingAdapter 和 BindingConversion 同时生效了，而 BindingConversion 的优先级要高些
    @BindingConversion
    public static String conversionString(String text) {
        return text + "-con";
    }

    @BindingConversion
    public static Drawable convertStringToDrawable(String str) {
        if (str.equals("红色")) {
            return new ColorDrawable(Color.parseColor("#FF4081"));
        }
        if (str.equals("蓝色")) {
            return new ColorDrawable(Color.parseColor("#3F51B5"));
        }
        return new ColorDrawable(Color.parseColor("#344567"));
    }

    @BindingConversion
    public static int convertStringToColor(String str) {
        if (str.equals("红色")) {
            return Color.parseColor("#FF4081");
        }
        if (str.equals("蓝色")) {
            return Color.parseColor("#3F51B5");
        }
        return Color.parseColor("#344567");
    }
}
